package com.changyue.activity.history;

import com.changyue.activity.history.entity.HistoryDataEntity;
import com.changyue.activity.history.entity.TestDataEntity;
import com.changyue.db.DaoUtilsStore;
import com.changyue.util.EmptyUtil;
import com.changyue.greendao.gen.HistoryDataEntityDao;
import java.util.List;

public class DataManager {

    public static List<HistoryDataEntity> getHistory() {
        List<HistoryDataEntity> list = DaoUtilsStore.getInstance().getHistoryDaoUtils().queryByNativeSql(" order by " + HistoryDataEntityDao.Properties.Id.columnName + " desc", new String[]{});
        return list;
    }

    public static HistoryDataEntity getHistory(Long historyId) {
        HistoryDataEntity historyDataEntity = DaoUtilsStore.getInstance().getHistoryDaoUtils().queryById(historyId);
        return historyDataEntity;
    }

    public static boolean saveHistory(HistoryDataEntity historyDataEntity) {
        if (EmptyUtil.isEmpty(historyDataEntity)) {
            return false;
        }

        Long historyId = DaoUtilsStore.getInstance().getHistoryDaoUtils().insert(historyDataEntity);
        if (EmptyUtil.isNotEmpty(historyDataEntity.lineDatas)) {
            for (TestDataEntity testDataEntity: historyDataEntity.lineDatas) {
                testDataEntity.setData_id(historyId);
                DaoUtilsStore.getInstance().getTestDataDaoUtils().insert(testDataEntity);
            }
        }
        return historyId != -1;
    }

    public static boolean delete(HistoryDataEntity historyDataEntity) {
        if (historyDataEntity == null) {
            return false;
        }

        DaoUtilsStore.getInstance().getTestDataDaoUtils().deleteMultiple(historyDataEntity.getLineDatas());

       boolean ret =  DaoUtilsStore.getInstance().getHistoryDaoUtils().delete(historyDataEntity);
        return ret;
    }

    public static void clearHistory() {
        DaoUtilsStore.getInstance().getHistoryDaoUtils().detachAll();
    }
}
